package org.example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test4 {
    //leetcode 三数之和
    //https://leetcode.cn/problems/1fGaJU/description/
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length;
        List<List<Integer>> ret = new ArrayList<>();
        if (n == 0) return ret;
        for (int i = 0; nums[i] <= 0 && i < n-2; i++) {
            int target = -nums[i];
            int left = i + 1, right = n - 1;
            if (i > 0 && nums[i] == nums[i-1]) continue;
            while (left < right) {
                if (nums[left] + nums[right] == target) {
                    List<Integer> list = new ArrayList<>();
                    list.add(nums[i]);
                    list.add(nums[left]);
                    list.add(nums[right]);
                    ret.add(list);
                    left++;
                    right--;
                }else if (nums[left] + nums[right] < target) {
                    left++;
                }else {
                    right--;
                }
                while (left > i+1 && left < right && nums[left] == nums[left-1]) left++;
                while (right < n-1 && left < right && nums[right] == nums[right+1]) right--;
            }
        }
        return ret;
    }

    public static void main(String[] args) {
        int[] arr = {-1,0,1,2,-1,-4,-2,-3,3,0,4};
        Test4 test4 = new Test4();
        test4.threeSum(arr);
        System.out.println(666);
    }
}
